﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.App.ViewModel.Base
{
    public class DataProcess<T> : ViewModelBase where T : class, new()
    {
        #region 属性
        private string searchText = string.Empty;
        /// <summary>
        /// 搜索内容
        /// </summary>
        public string SearchText
        {
            get { return searchText; }
            set { searchText = value; RaisePropertyChanged(); }
        }


        private ObservableCollection<T> gridModelList;
        /// <summary>
        /// 抽象表单数据
        /// </summary>
        public ObservableCollection<T> GridModelList
        {
            get { return gridModelList; }
            set { gridModelList = value; RaisePropertyChanged(); }
        }

        private T model;
        /// <summary>
        /// 操作实体
        /// </summary>
        public T Model
        {
            get { return model; }
            set
            {
                model = value;
                this.RaisePropertyChanged("Model");
            }
        }
        #endregion

        #region 命令
        public RelayCommand AddCommand
        {
            get; private set;
        }

        public RelayCommand QueryCommand
        {
            get; private set;
        }

        public RelayCommand SearchCommand
        {
            get; private set;
        }

        public RelayCommand<T> ShowInfoCommand
        {
            get; private set;
        }

        public RelayCommand<T> EditCommand
        {
            get; private set;
        }

        public RelayCommand<T> DelCommand
        {
            get; private set;
        }
        #endregion


        public virtual void Init()
        {
            GridModelList = new ObservableCollection<T>();
            InitCommand();

        }

        private void InitCommand()
        {
            EditCommand = new RelayCommand<T>(t => Edit(t));
            DelCommand = new RelayCommand<T>(t => Del(t));
            QueryCommand = new RelayCommand(Query);
            SearchCommand = new RelayCommand(Search);
            AddCommand = new RelayCommand(Add);
            ShowInfoCommand = new RelayCommand<T>(ShowInfo);
        }

        #region IDataOperation

        /// <summary>
        /// 新增
        /// </summary>
        public virtual void Add(T model)
        {
            Model = new T();
        }

        /// <summary>
        /// 新增
        /// </summary>
        public virtual void Add()
        {
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public virtual void Edit(T model)
        {
            Model = (model as T);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public virtual void Del(T model) { }

        /// <summary>
        /// 查询
        /// </summary>
        public virtual void Query()
        {
            this.GetPageData(this.PageIndex);
        }

        public virtual void Search()
        {

        }

        public virtual void ShowInfo(T model)
        {

        }

        /// <summary>
        /// 重置
        /// </summary>
        public virtual void Reset()
        {
            this.SearchText = string.Empty;
        }

        /// <summary>
        /// 保存
        /// </summary>
        public virtual void Save()
        {
        }

        /// <summary>
        /// 返回
        /// </summary>
        public virtual void Cancel()
        {
        }


        #endregion

        #region 分页

        public RelayCommand GoHomePageCommand { get { return new RelayCommand(() => GoHomePage()); } }

        public RelayCommand GoOnPageCommand { get { return new RelayCommand(() => GoOnPage()); } }

        public RelayCommand GoNextPageCommand { get { return new RelayCommand(() => GoNextPage()); } }

        public RelayCommand GoEndPageCommand { get { return new RelayCommand(() => GoEndPage()); } }


        private int totalCount = 0;
        private int pageSize = 15;
        private int pageIndex = 1;
        private int pageCount = 0;

        /// <summary>
        /// 总数
        /// </summary>
        public int TotalCount { get { return totalCount; } set { totalCount = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 当前页大小
        /// </summary>
        public int PageSize { get { return pageSize; } set { pageSize = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get { return pageIndex; } set { pageIndex = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 分页总数
        /// </summary>
        public int PageCount { get { return pageCount; } set { pageCount = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 首页
        /// </summary>
        public virtual void GoHomePage()
        {
            if (this.PageIndex == 1) return;

            PageIndex = 1;

            GetPageData(PageIndex);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public virtual void GoOnPage()
        {
            if (this.PageIndex == 1) return;

            PageIndex--;

            this.GetPageData(PageIndex);
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public virtual void GoNextPage()
        {
            if (this.PageIndex == PageCount) return;

            PageIndex++;

            this.GetPageData(PageIndex);
        }

        /// <summary>
        /// 尾页
        /// </summary>
        public virtual void GoEndPage()
        {
            this.PageIndex = PageCount;

            GetPageData(PageCount);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex"></param>
        public virtual void GetPageData(int pageIndex) { }

        /// <summary>
        /// 设置页数
        /// </summary>
        public virtual void SetPageCount()
        {
            PageCount = Convert.ToInt32(Math.Ceiling((double)TotalCount / (double)PageSize));
        }

        #endregion
    }
}
